<?php
namespace PHPMailer;

/*
 * author:lovefc
 * time:2016    /9/8
 */

class MailSend
{
    
    public $obj;
	
    public $SMTPSecure;
        
    public $CharSet ;
        
    public $SMTPAuth;
        
    public $Port    ;
        
    public $Host    ;
        
    public $Username;
        
    public $Password;
        
    public $Form;
        
    public $Formname;
        
    public $Replyto;
        
    public $Replyname;	
	
    
	public function __construct($config = null){
		$this->obj = new PHPMailer();
		if($config){
			$this->config($config);
		}
	}
	
    //配置判断
    public function config($config)
    {
        $this->SMTPSecure = isset($config['smtp_secure'])? $config['smtp_secure'] : '';//认证
        
        $this->CharSet  = isset($config['smtp_charset']) ? $config['smtp_charset'] : 'UTF-8';//设置邮件的字符编码，这很重要，不然中文乱码
        
        $this->SMTPAuth = isset($config['smtp_auth']) && !empty($config['smtp_auth']) ? true : false; //开启认证
        
        $this->Port     = isset($config['smtp_port']) ? $config['smtp_port'] : ''; //端口
        
        $this->Host     = isset($config['smtp_host']) ? $config['smtp_host'] : ''; //服务端地址
        
        $this->Username = isset($config['smtp_user']) ? $config['smtp_user'] : ''; //用户邮箱
        
        $this->Password = isset($config['smtp_pass']) ? $config['smtp_pass'] : ''; //用户密码
        
        $this->Form = isset($config['form']) ? $config['form'] : ''; //发送邮箱
        
        $this->Formname = isset($config['form_name']) ? $config['form_name'] : ''; //发送人姓名
        
        $this->Replyto = isset($config['replyto']) ? $config['replyto'] : ''; //回复邮箱
        
        $this->Replyname = isset($config['reply_name']) ? $config['reply_name'] : ''; //回复人姓名
    }
    
    public function Send($to, $title, $body, $file = null, $lenght = 80)
    {
        $mail   = $this->obj;
		
		if(!empty($this->SMTPSecure)){
			$mail->SMTPSecure = $this->SMTPSecure;
		}
	
        $mail->CharSet =$this->CharSet; //编码要指定

        $mail->Encoding = 'base64';

        $mail->IsSMTP(); // 代表我们使用SMTP的方式

        $mail->Port     = $this->Port; //端口

        $mail->Host     = $this->Host; //服务端地址

        $mail->SMTPAuth =  $this->SMTPAuth;//服务器认证

        $mail->SMTPDebug  = false;

        $mail->Username = $this->Username; //用户邮箱

        $mail->Password = $this->Password; //用户密码

        $mail->From     = $this->Form; //发送的邮箱

        $mail->FromName = $this->Formname; //发送人

        //$mail->IsSendmail(); //如果没有sendmail组件就注释掉，否则出现“Could  not execute: /var/qmail/bin/sendmail ”的错误提示
        $replyto = isset($this->Replyto) ? $this->Replyto : $this->Form;
            
        $replyname = isset($this->Replyname) ? $this->Replyname : $this->Formname;
            
        $mail->AddReplyTo($replyto, $replyname); //回复地址，//回复人		
		
        $mail->AddAddress($to);

        $mail->Subject = $title;

        $mail->Body    = $body;
		
        //$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //当邮件不支持html时备用显示，可以省略
		
        //$mail->WordWrap = 80; // 设置每行字符串的长度
		
        //$mail->AddAttachment("f:/test.png"); //可以添加附件
		
        $mail->IsHTML(true);
        if ($mail->Send()) {
            return true;
        } else {
            return false;
        }
    }
}
